package com.liliudong.template.api.admin.controller.sys;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.liliudong.template.biz.service.sys.SysUserService;
import com.liliudong.template.common.R;
import com.liliudong.template.common.constant.ExMessage;
import com.liliudong.template.common.exception.BizException;
import com.liliudong.template.common.request.sys.SysUserAddRequest;
import com.liliudong.template.common.request.sys.SysUserModifyRequest;
import com.liliudong.template.common.response.sys.SysUserBaseResponse;
import com.liliudong.template.common.response.sys.SysUserDetailResponse;
import com.liliudong.template.common.response.sys.SysUserPageResponse;
import com.liliudong.template.common.response.sys.SysUserProfileResponse;
import com.mybatisflex.core.paginate.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 用户控制器
 *
 * @author liudo
 * @date 2024/07/30
 */
@RestController
@RequestMapping("/sys/user")
@RequiredArgsConstructor
public class SysUserController {

    private final SysUserService sysUserService;

    /**
     * 基本信息
     *
     * @return {@link R }<{@link SysUserProfileResponse }>
     */
    @SaCheckLogin
    @GetMapping("/profile")
    public R<SysUserProfileResponse> profile() {
        SysUserProfileResponse sysUserProfileResponse = sysUserService.profile();
        return R.ok(sysUserProfileResponse);
    }

    /**
     * 添加用户
     *
     * @param sysUserAddRequest sys用户添加dto
     * @return {@link R }<{@link ? }>
     */
    @SaCheckPermission("sys:user:add")
    @PostMapping
    public R<?> addOne(@RequestBody @Validated SysUserAddRequest sysUserAddRequest) {
        sysUserService.addOne(sysUserAddRequest);
        return R.ok();
    }

    /**
     * 按ID删除
     *
     * @param id 身份证件
     * @return {@link R }<{@link ? }>
     */
    @SaCheckPermission("sys:user:delete")
    @DeleteMapping("/{id}")
    public R<?> delete(@PathVariable Long id) {
        if (id == 1L) {
            throw new BizException(ExMessage.CANNOT_DELETE_ADMIN);
        }
        sysUserService.removeById(id);
        return R.ok();
    }

    /**
     * 按ID修改
     *
     * @param id               身份证件
     * @param sysUserModifyRequest sys用户添加或更新dto
     * @return {@link R }<{@link ? }>
     */
    @SaCheckPermission("sys:user:modify")
    @PutMapping("/{id}")
    public R<?> modifyById(@PathVariable Long id, @RequestBody SysUserModifyRequest sysUserModifyRequest) {
        sysUserService.modifyById(id, sysUserModifyRequest);
        return R.ok();
    }

    /**
     * 分页查询
     *
     * @param page 页码
     * @param size 大小
     */
    @SaCheckPermission("sys:user:list")
    @GetMapping("/page/{page}/{size}")
    public R<Page<SysUserPageResponse>> page(@PathVariable Long page,
                                             @PathVariable Long size) {
        Page<SysUserPageResponse> pageData = sysUserService.queryPage(page, size);
        return R.ok(pageData);
    }

    /**
     * 按id获取
     *
     * @param id 身份证件
     * @return {@link R }<{@link SysUserBaseResponse }>
     */
    @SaCheckPermission("sys:user:detail")
    @GetMapping("/detail/{id}")
    public R<SysUserDetailResponse> detailById(@PathVariable Long id) {
        SysUserDetailResponse sysUserDetailVO = sysUserService.detailById(id);
        return R.ok(sysUserDetailVO);
    }

}
